//---------------------------------------------------------------------
// File: Pop3Component.cs
// 
// Summary: 
//
//---------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

namespace Microsoft.Services.BizTalkApplicationFramework.BizUnit.Pop3
{
	using System;
	using System.IO;
	using System.Text;
	using System.Text.RegularExpressions;

	/// <summary>
	/// Summary description for Pop3Attachment.
	/// </summary>
	internal class Pop3Component
	{
		private string m_contentType;
		private string m_name;
		private string m_filename;
		private string m_contentTransferEncoding;
		private string m_contentDescription;
		private string m_contentDisposition;
		private string m_data;
			
		internal byte[] m_binaryData;

		internal string FileExtension
		{
			get 
			{
				string extension = null;

				// if file has a filename and the filename
				// has an extension ...

				if( (m_filename != null) && 
					Regex.Match(m_filename,@"^.*\..*$").Success)
				{
					// get extension ...
					extension =  
					Regex.Replace(m_name,@"^[^\.]*\.([^\.]+)$","$1");
				}

				// NOTE: return null if extension
				// not found ...
				return extension;
			}
		}

		internal string FileNoExtension
		{
			get 
			{
				string extension = null;

				// if file has a filename and the filename
				// has an extension ...

				if( (m_filename != null) && 
					Regex.Match(m_filename,@"^.*\..*$").Success)
				{
					// get extension ...
					extension =  
						Regex.Replace(m_name,@"^([^\.]*)\.[^\.]+$","$1");
				}

				// NOTE: return null if extension
				// not found ...
				return extension;
			}
		}

		internal string Filename
		{
			get { return m_filename; }
		}

		internal string ContentType
		{
			get { return m_contentType; }
		}

		internal string Name
		{
			get { return m_name; }
		}

		internal string ContentTransferEncoding
		{
			get { return m_contentTransferEncoding; }
		}

		internal string ContentDescription
		{
			get { return m_contentDescription; }
		}

		internal string ContentDisposition
		{
			get { return m_contentDisposition; }
		}

		internal string Data
		{
			get { return m_data; }
		}

		public override string ToString()
		{
			return 
				"Content-Type: "+m_contentType + "\r\n" +
				"Name: "+m_name + "\r\n" +
				"Filename: "+m_filename+"\r\n"+
				"Content-Transfer-Encoding: "+m_contentTransferEncoding+"\r\n"+
				"Content-Description: "+m_contentDescription+"\r\n"+
				"Content-Disposition: "+m_contentDisposition+"\r\n"+
				"Data :" +m_data;
		}


		internal bool IsBody
		{
			get 
			{ return
				(m_contentDisposition==null)?true:false; 
			}
		}

		internal bool IsAttachment
		{
			get 
			{ 
				bool ret = false;

				if(m_contentDisposition != null)
				{
					ret =
						Regex
						.Match(m_contentDisposition,
						"^attachment.*$")
						.Success;
				}

				return ret;
			}
		}

		private void DecodeData()
		{
			// if this data is an attachment ...
				// if BASE-64 data ...
			if (m_contentTransferEncoding != null)
			{
				if( m_contentTransferEncoding.ToUpper()
					.Equals("BASE64") )
				{
					// convert attachment from BASE64 ...
					m_binaryData = 
						Convert.FromBase64String(m_data.Replace("\n",""));

					m_data = System.Text.ASCIIEncoding.ASCII.GetString(m_binaryData);
				}
				else
					// if PRINTABLE ...
					if( 
					m_contentTransferEncoding.ToUpper()
					.Equals("QUOTED-PRINTABLE") )
				{
					m_data = Pop3Statics.FromQuotedPrintable(m_data);
				}
			}
		}

		internal Pop3Component(string contentType, string contentTransferEncoding, string data)
		{
			m_contentTransferEncoding = contentTransferEncoding;
			m_contentType = contentType;
			m_data = data;

			m_data = m_data.Substring(0, m_data.Length-2);
			DecodeData();
		}

		internal Pop3Component(string contentType, string name, string filename, 
			string contentTransferEncoding, string contentDescription,
			string contentDisposition, string data)
		{
			m_contentType = contentType;
			m_name = name;
			m_filename = filename;
			m_contentTransferEncoding = contentTransferEncoding;
			m_contentDescription = contentDescription;
			m_contentDisposition = contentDisposition;
			m_data = data;

			DecodeData();
		}
	}
}
